/**
 * @name 权限
 */
layui.define(['larry', 'form', 'table', 'treeGrid'], function(exports) {
    "use strict";
    var $ = layui.$,
        larry = layui.larry,
        form = layui.form,
        table = layui.table,
        treeGrid = layui.treeGrid;
    var tableIns = treeGrid.render({
        elem: '#permissionsList', //指定原始表格元素选择器（推荐id选择器）
        cols: [
            [{
                field: 'name',
                title: '名称',
            }, {
                field: 'permissionValue',
                align: 'center',
                title: '权限值',
            }, {
                field: 'uri',
                align: 'center',
                title: '路径',
            }, {
                field: 'icon',
                align: 'center',
                title: '图标',
                templet: '#iconTpl',
            }, {
                field: 'status',
                align: 'center',
                title: '状态',
                templet: '#statusTpl',
            }, {
                align: 'center',
                title: '操作',
                toolbar: '#permissionsBar'
            }]
        ], //设置表头
        url: '/backend/system/upms/permissions/findPagingQuery',
        treeId: 'permissionId', //树形id字段名称
        treeUpId: 'pid', //树形父id字段名称
     
        treeShowName: 'name', //以树形式显示的字段
        page: false
    });
    //监听锁定操作
    form.on('checkbox(status)', function(obj) {
        $.post("/backend/system/upms/permissions/updataDisablePermissionId", {
            "permissionId": this.value
        }, function(result) {
            if (result.status != 200) {
                layer.tips(result.mgs);
                tableIns.reload();
            }
        });
    });
    //添加根菜单
    $('#addPermissions').on('click', function() {
        layer.open({
            type: 2,
            title: '添加根菜单',
            shadeClose: true,
            shade: 0.8,
            area: ['100%', '100%'],
            content: '/backend/system/upms/permissions/add', //iframe的url
            end: function() {
                tableIns.reload();
            }
        });
    });
    //监听工具条
    treeGrid.on('tool(permissionsList)', function(obj) {
        var data = obj.data;
        if (obj.event === 'detail') {
            //添加子菜单
            layer.open({
                type: 2,
                title: '图标选择',
                shadeClose: true,
                shade: 0.8,
                area: ['100%', '100%'],
                content: '/backend/system/upms/permissions/addPermissionsIndex?permissionId=' + data.permissionId + '&pidName=' + data.name,
                end: function() {
                    tableIns.reload();
                }
            });
        } else if (obj.event === 'del') {
            //删除        
            layer.confirm('真的删除行么', function(index) {
                $.post("/backend/system/upms/permissions/deletePermission", {
                    "permissionId": data.permissionId
                }, function(result) {
                    if (result.status == 200) {
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                    layer.close(index);
                });
            });
        } else if (obj.event === 'edit') {
        		//编辑
            layer.open({
                type: 2,
                title: '图标选择',
                shadeClose: true,
                shade: 0.8,
                area: ['100%', '100%'],
                content: '/backend/system/upms/permissions/edit?permissionId=' + data.permissionId,
                end: function() {
                    tableIns.reload();
                }
            });
        }
    });
    exports('mypanel', {});
});